package com.ruoyi.web.controller.drugSystem.drugInfo;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.drugSystem.domain.SysDrugStock;
import com.ruoyi.drugSystem.service.ISysDrugStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仓库Controller
 *
 * @author ruoyi
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/system/stock")
public class SysDrugStockController extends BaseController
{
    @Autowired
    private ISysDrugStockService sysDrugStockService;

    /**
     * 查询仓库列表
     */
    @PreAuthorize("@ss.hasPermi('system:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDrugStock sysDrugStock)
    {
        startPage();
        List<SysDrugStock> list = sysDrugStockService.selectSysDrugStockList(sysDrugStock);
        return getDataTable(list);
    }

    /**
     * 导出仓库列表
     */
    @PreAuthorize("@ss.hasPermi('system:stock:export')")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDrugStock sysDrugStock)
    {
        List<SysDrugStock> list = sysDrugStockService.selectSysDrugStockList(sysDrugStock);
        ExcelUtil<SysDrugStock> util = new ExcelUtil<SysDrugStock>(SysDrugStock.class);
        util.exportExcel(response, list, "仓库数据");
    }

    /**
     * 获取仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:stock:query')")
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") Long stockId)
    {
        return success(sysDrugStockService.selectSysDrugStockByStockId(stockId));
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("@ss.hasPermi('system:stock:add')")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDrugStock sysDrugStock)
    {
        if (!sysDrugStockService.checkDrugStockNameUnique(sysDrugStock))
        {
            return error("新增仓库'" + sysDrugStock.getStockName() + "'失败，仓库名称已存在");
        }
        else if (!sysDrugStockService.checkDrugStockCodeUnique(sysDrugStock))
        {
            return error("新增仓库'" + sysDrugStock.getStockName() + "'失败，仓库编码已存在");
        }
        return toAjax(sysDrugStockService.insertSysDrugStock(sysDrugStock));
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("@ss.hasPermi('system:stock:edit')")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDrugStock sysDrugStock)
    {
        if (!sysDrugStockService.checkDrugStockNameUnique(sysDrugStock))
        {
            return error("修改仓库'" + sysDrugStock.getStockName() + "'失败，仓库名称已存在");
        }
        else if (!sysDrugStockService.checkDrugStockCodeUnique(sysDrugStock))
        {
            return error("修改仓库'" + sysDrugStock.getStockName() + "'失败，仓库编码已存在");
        }
        return toAjax(sysDrugStockService.updateSysDrugStock(sysDrugStock));
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("@ss.hasPermi('system:stock:remove')")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable Long[] stockIds)
    {
        return toAjax(sysDrugStockService.deleteSysDrugStockByStockIds(stockIds));
    }
}